function TreeNode(val, left, right) {
  this.val = val === undefined ? 0 : val;
  this.left = left === undefined ? null : left;
  this.right = right === undefined ? null : right;
}

const root = new TreeNode(
  1,
  new TreeNode(2, new TreeNode(3), new TreeNode(4)),
  new TreeNode(2, null, new TreeNode(3))
);

function isSymmetric(root) {
  if (!root) return true;
  return compare(root.left, root.right);

  function compare(left, right) {
    // 双空视为对称
    if (!left && !right) return true;
    // 结构不对称直接返回
    if (!left || !right) return false;
    // 值不相等返回false
    if (left.val !== right.val) return false;
    // 递归比较镜像节点
    return compare(left.left, right.right) && compare(left.right, right.left);
  }
}
